<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test importing an RSA key for RSASSA-PKCS1-v1_5.");

jsTestIsAsync = true;

var extractable = true;

var publicKeyJSON = {
    kty: "RSA",
    alg: "RS256",
    n: Base64URL.stringify(hexStringToUint8Array("ADC0940AFECE7351D56A6D432210B3AA49D38566B03A9F102E4F198B2DA9D740728D01426A3A058B2B805A5F91D565D969FE318AD2D1ADA713F5A829CC8CDCF8C6CB4872068164063B6D651A2226CB97ED67E0FC6C702A473DB2D79A730F8738084A2EED74922C3A119D1D101B932C0E10FAB36815F66C0792BB640B1B4C59D062FBBEDAB3CC069A535195D70E4A06432CAF149C24A00353A0B99F7CF5B17273CB4E38421BD315127CF4B3DCB3D20A7C98CFAF1A0E398A55E347FA283CE7B39273259B1B2132DC18B0EB8AAE9F78EE525356B09DF39E090E76D7985B2B71E50AF85CA36CE91F8CCB2ABBD8A529D369890D98A2CA2825C4C2FF8B7FBF09E79C0B")),
    e: Base64URL.stringify(hexStringToUint8Array("010001")),
};

var privateKeyJSON = {
    kty: "RSA",
    alg: "RS256",
    n: Base64URL.stringify(hexStringToUint8Array("ADC0940AFECE7351D56A6D432210B3AA49D38566B03A9F102E4F198B2DA9D740728D01426A3A058B2B805A5F91D565D969FE318AD2D1ADA713F5A829CC8CDCF8C6CB4872068164063B6D651A2226CB97ED67E0FC6C702A473DB2D79A730F8738084A2EED74922C3A119D1D101B932C0E10FAB36815F66C0792BB640B1B4C59D062FBBEDAB3CC069A535195D70E4A06432CAF149C24A00353A0B99F7CF5B17273CB4E38421BD315127CF4B3DCB3D20A7C98CFAF1A0E398A55E347FA283CE7B39273259B1B2132DC18B0EB8AAE9F78EE525356B09DF39E090E76D7985B2B71E50AF85CA36CE91F8CCB2ABBD8A529D369890D98A2CA2825C4C2FF8B7FBF09E79C0B")),
    e: Base64URL.stringify(hexStringToUint8Array("010001")),
    d: Base64URL.stringify(hexStringToUint8Array("78D2D2DFB682CFB45748D3C3FC3B4B049EA3E53F1C4B17734410A33DA23A59C1AA269D7A96ADD44F0BA80CB02A940F681989B6DFC76C216A6EB9C3FF950B5B59EFBB4A9C6823AFEF99819FED85541EFD7E0C5F6A88E992ACC99DC4C9D53B361763C45A4FAB613E39FF27971F2D5DB123F2F0B2B32D9A9752C8D9605F4CD3521C2DB9A86000A3040A16231F61278C7747C3AB18DBEC00A5D71023B50B134A28E51CDB419A66BA170DF618817342F7861174AA8E7499B974DBFEB9FC643B0E95645C1C4D81104BCE53DA6E6CF31B6A0085F79F2E0626482D68A5A0A45C4EAF45640D58D2F8E5164D64560CDEEA0982138CA867A870CD01A63742847ACF3A516F49")),
    p: Base64URL.stringify(hexStringToUint8Array("E26893B808CA31E1F9B89E4A077F7B414C216E46048126DC0369A27E64A4BC4DB4D7C81BE79AA40472B5795AF27F5FE6E372CD95CE8EFDA93A81FCDD64866F4B93421A33E5D2AD3DA65529BBCA85392B2907003AEEA187EFA9459592E7FDDE328254BBC1F2EBBB8FF962CF2A2CBB3E0EA54BBE2C29A2C3A7CB25AAC0CE405E9D")),
    q: Base64URL.stringify(hexStringToUint8Array("C4762BCE48AFB669F3FEC19C867586734ABEA433A42A2729B51A6FDA93052085F19C55F968C7845C866356E8D7B70532D5494520DD866494AFCB9289EFD9AEFD7C8D9C51CCCDE747F80DDE7B7BBC875514AD917EBD4BB5FDEE14FF1EBB34DD5D79DCD448ACA737BA459773A4B9284C056195F9CC3338F75C5407D5B9997150C7")),
    dp: Base64URL.stringify(hexStringToUint8Array("7C1CC3CD80D40660501A8A7B1E7D1DBDFFD3DBB57A46AA5CB56A34EF809065C15B3F66AD155B4A4A3DEF8964F7C62776547CDC8220C775FA4CDE71159443B5C302289C6092BDD8C610E66288554E8EB640395C40F20B8E8F2C85BEA6D791ED3A5703C8215BDF29E494DB3BA16CFFFFD6DB7A17F7F1A30E0A6F6C689C64F41869")),
    dq: Base64URL.stringify(hexStringToUint8Array("8E61360E220F761C0380B5C042921A06A43CD5B3B75DF553FCB4542C0C30C3094FB90575E3C1F4E33961F5327A6361991D8A2457553478E0692717E46FB74B62DA49A6E8818D17F8C88504A06964908FD09499C5485AFE62319175F347A96064C6548C64BEFBD11F662004E7746907EF3E71FABF4AC016450B81421B6860FF31")),
    qi: Base64URL.stringify(hexStringToUint8Array("6025FFA4BC1B3010354E1547D167309A7CADA8D72B3021304E6EC1C80D9E53A9D66D0AD42EF7FB9BDFE4CDF2D4723B27A405650501B924C5CCCB44AAE29B706F2C2C6B9F86F0A00E38E4762F7BF8842F84CACF5FB6014CFF78B4A38D1E7901F86F785A3DB9EDAC878C83C23394EB9560A7FDFB642E283A1679175DECDB1DC539"))
};

var data = asciiToUint8Array("Hello, world!");

debug("Importing a public key...");
crypto.subtle.importKey("jwk", publicKeyJSON, { name: "RSASSA-PKCS1-v1_5", hash: {name: "SHA-256"} }, extractable, ["verify"]).then(function(result) {
    publicKey = result;
    debug("\nImporting a private key...");
    return crypto.subtle.importKey("jwk", privateKeyJSON, { name: "RSASSA-PKCS1-v1_5", hash: {name: "SHA-256"} }, extractable, ["sign"]);
}).then(function(result) {
    privateKey = result;
    debug("\nSigning some text...");
    return crypto.subtle.sign({ name: "RSASSA-PKCS1-v1_5"}, privateKey, data);
}).then(function(result) {
    signature = result;
    shouldBe("bytesToHexString(new Uint8Array(signature))", "'0fd9a8aef4cc1876c0b762545336c6d1fb315ae16ae4b5e4bf34d384d8585ea7a01e76ea09ee7f7ee8d1c122e7dd15b7c94a573b2aa07203e8d13bc6fd16156cd8e5f0c15a15dccb62d152127fca09882fb53bc3e60ab586d15b95cf411e3aab4a1c231a7e91aab09ee3d4b13d11e97505ddff77683470da510ee76e8bd530c56a85f901626a5a710f716f113dfe9cf6c473ee16fa248aea3480a1033abe30f4c1243289a661e64d7818b55698280688097135968c6d4b029496d85cab2a67e4696737781f70e4392c7df71bbd6c92465947f029a1de48160aced11b5721b1cd25039fe2c16c2b38de73df3b9a83e3ea755fd0cfe51ca06b61fadf6d84677f95'");

    debug("\nVerifying the signature...");
    return crypto.subtle.verify({ name: "RSASSA-PKCS1-v1_5" }, publicKey, signature, data);
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "true");
    debug("\nVerifying a bad signature...");
    return crypto.subtle.verify({ name: "RSASSA-PKCS1-v1_5" }, publicKey, new Uint8Array(256), data);
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "false");
}).then(finishJSTest, failAndFinishJSTest);
</script>

</body>
</html>
